<?php
class Mcabinprice extends CI_Model{
		protected $_table = "tbl_tickets";
		protected $_room  = "tbl_rooms";
		protected $db;
		public function __construct(){
			parent::__construct();
			$this->db=$this->load->database("default",TRUE);
		}
		public function getdata($id){
			$this->db->where("id",$id);
			$r=$this->db->get($this->_table)->row_array();
			if(count($r)>0){
				return $r;
			}else{
				return array("id"=>"0",
							"cabinid"=>"0",
							"pricedate"=>"",
							"priceold"=> "0",
							"pricedouble"=>"0",
							"pricetwin"=>"0",
							"pricesingle"=>"0",
							"pricechild"=>"0",
							"priceinfant"=>"0",
							"numberavai"=>"0",
							"numbernight"=>"0",
							"cabinid"=>"0");
   
			}
		}
		public function listall(){
			return $this->db->get($this->_table)->result_array();
		}
		public function getcabins($cruise_id){
			$this->db->where("cruise_id",$cruise_id);
			return $this->db->get($this->_room)->result_array();
		}
		public function getdatabycabinidadndate($cabinid,$fromdate,$todate,$numbernight){
			$this->db->where("cabinid",$cabinid);
			$this->db->where("pricedate>=",$fromdate);
			$this->db->where("pricedate>=",$todate);
			$this->db->where("numbernight",$numbernight);
			$this->db->order_by("pricedate","asc");
			return $this->db->get($this->_table)->result_array();
		}
		public function add($data){
        	$this->db->insert($this->_table,$data);
		}
		public function del($id){
			$this->db->where("id",$id);
			$this->db->delete($this->_table);
		}
		public function update($data){
			$this->db->query("update cabinprice set pricedate='".$data["pricedate"]."',priceold='".$data["priceold"]."',pricedouble='".$data["pricedouble"]."',pricetwin='".$data["pricetwin"]."',pricesingle='".$data["pricesingle"]."',pricechild='".$data["pricechild"]."',priceinfant='".$data["priceinfant"]."',numberavai='".$data["numberavai"]."',numbernight='".$data["numbernight"]."' where id='".$data["id"]."'");
		}
   public function updatenumberavaiable($numberavaible,$id){
   	$this->db->query("update cabinprice set numberavai='$numberavaible'  where id='".$id."'");
	
   }
   public function updatenumberavaiablebyquery($listcabinid,$listnumbernight,$listdate,$numberavaiable){
   	$this->db->query("update cabinprice set numberavai='$numberavaiable'  where cabinid in($listcabinid) and numbernight in ($listnumbernight) and pricedate in ($listdate)");
   }
   public function updatesmart($date,$numbernight,$cabinid,$data){
   		$olddata=$this->db->query("select * from cabinprice where pricedate='$date' and numbernight='$numbernight' and cabinid='$cabinid'");
		if($olddata->num_rows()>0){
			$updatedata=$olddata->row_array();
			$updatedata["priceold"]=$data["priceold"];
			$updatedata["pricedouble"]=$data["pricedouble"];
			$updatedata["pricetwin"]=$data["pricetwin"];
			$updatedata["pricesingle"]=$data["pricesingle"];
			$updatedata["pricechild"]=$data["pricechild"];
			$updatedata["priceinfant"]=$data["priceinfant"];
			$updatedata["numberavai"]=$data["numberavai"];
			$this->update($updatedata);
		}else{
			$this->db->query("insert into cabinprice (pricedate,priceold,pricedouble,pricetwin,pricesingle,pricechild,priceinfant,numberavai,numbernight,cabinid) values('".$data["pricedate"]."','".$data["priceold"]."','".$data["pricedouble"]."','".$data["pricetwin"]."','".$data["pricesingle"]."','".$data["pricechild"]."','".$data["priceinfant"]."','".$data["numberavai"]."','".$data["numbernight"]."','".$data["cabinid"]."')");
		}
   }
	public function getAllByDateAndCabinCode($date,$trainid,$type=1){
		$data= $this->db->query("select * from ".$this->_table." where tickdate='$date' and trainid ='$trainid' and ticktype='$type'");
		if($data->num_rows()>0){
			return $data->row_array();
		}else 
		return array("id"=>"0",
			"cabinid"=>"0",
			"pricedate"=>"",
			"priceold"=> "0",
			"pricedouble"=>"0",
			"pricetwin"=>"0",
			"pricesingle"=>"0",
			"pricechild"=>"0",
			"priceinfant"=>"0",
			"numberavai"=>"0",
			"numbernight"=>"0",
			"cabinid"=>"0");
	   }
	   
	   public function updaterate($date,$cabinid,$data){
		    $olddata= $this->db->query("select * from  tbl_checkrate where rate_date='$date' and cabinid='$cabinid'");
			if($olddata->num_rows()>0){
				//return "> 0";
				$updatedata=$olddata->row_array();
				//print_r($updatedata);
				//die();
				$update["rate_priceold"]=$data["rate_priceold"];
				$update["rate_priceone"]=$data["rate_priceone"];
				$update["rate_pricetwo"]=$data["rate_pricetwo"];
				$update["rate_extra"]=$data["rate_extra"];
				$update["rate_numrom"]=$data["rate_numrom"];
				$update["rate_date"]=$data["rate_date"];
				$update["cabinid"]=$data["cabinid"];
				$this->db->where("rate_date",$update["rate_date"]);
				$this->db->update("tbl_checkrate",$update);
			}else{
				//return "< 0";
				$this->db->query("insert into tbl_checkrate(rate_priceold,rate_priceone,rate_pricetwo,rate_extra,rate_date,rate_numrom,cabinid) values('".$data["rate_priceold"]."','".$data["rate_priceone"]."','".$data["rate_pricetwo"]."','".$data["rate_extra"]."','".$data["rate_date"]."','".$data['rate_numrom']."','".$data["cabinid"]."')");
			}
	   }
	}